/*
 * Copyright 2009 Codecarpet
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.chalmers.foa.gui;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.chalmers.foa.R;
import com.codecarpet.fbconnect.FBFeedActivity;
import com.codecarpet.fbconnect.FBSession;

public class LevelFinished extends Activity {

	// /////////////////////////////////////////////////////////////////////////////////////////////////
	// This application will not work until you enter your Facebook
	// application's API key here:

	private static final String API_KEY = "4da7d451077b85e35a4f237a8bd33bd4"; // "<YOUR API KEY>";
	// Enter either your API secret or a callback URL (as described in
	// documentation):
	private static final String API_SECRET = "d83584c718a2d03dab572a6fd96d3fa9"; // "<YOUR SECRET KEY>";

	private static final int MESSAGE_PUBLISHED = 2;
	// /////////////////////////////////////////////////////////////////////////////////////////////////

	private FBSession mSession;
	private TextView userTimeLabel, devTimeLabel, levelLabel;
	private Button mFeedButton, nextLevel, restartLevel;
	private String level;
	private float time, devTime;
	private boolean facebookEnabled;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// Forces
																				// landscape
																				// mode
		setResult(0);
		Bundle extras = getIntent().getExtras();
		if (extras != null && !extras.isEmpty()) {
			level = extras.getString("level");
			time = extras.getFloat("time");
			devTime = extras.getFloat("devTime");
			facebookEnabled = extras.getBoolean("facebook");
		} else {
			level = "Level 2: The Lions Gap";
			time = 1338f;
			devTime = 1337f;
			facebookEnabled = false;
		}

		setContentView(R.layout.facebook);
		mFeedButton = (Button) findViewById(R.id.feedButton);
		if (facebookEnabled) {
			mFeedButton.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {
					mSession = FBSession.getSessionForApplication_secret(
							API_KEY, API_SECRET, null);
					mSession.resume(LevelFinished.this);
					publishFeedWithPrompt();
				}
			});
		} else
			mFeedButton.setVisibility(View.INVISIBLE);

		levelLabel = (TextView) findViewById(R.id.win_level_name);
		userTimeLabel = (TextView) findViewById(R.id.win_user_time);
		devTimeLabel = (TextView) findViewById(R.id.win_dev_time);

		levelLabel.setText(level);
		userTimeLabel.setText(userTimeLabel.getText().toString() + time);
		devTimeLabel.setText(devTimeLabel.getText().toString() + devTime);

		nextLevel = (Button) findViewById(R.id.nextButton);
		nextLevel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				setResult(1);
				finish();
			}
		});
		restartLevel = (Button) findViewById(R.id.restartButton);
		restartLevel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////

	@Override
	public void onBackPressed() {
		setResult(2);
		super.onBackPressed();
	}

	private void publishFeedWithPrompt() {
		Intent intent = new Intent(this, FBFeedActivity.class);
		intent.putExtra("userMessagePrompt", "Brag about your time:");
		intent
				.putExtra(
						"attachment",
						"{\"name\":\"Force of Attraction\",\"href\":\"http://code.google.com/p/tda366-forceofattraction/\",\"caption\":\""
								+ level
								+ "\",\"description\":\"Time: "
								+ time
								+ " seconds\",\"media\":[{\"type\":\"image\",\"src\":\"http://photos-d.ak.fbcdn.net/photos-ak-sf2p/v27562/151/117702484935235/app_1_117702484935235_5887.gif\",\"href\":\"http://developers.facebook.com/\"}]}");
		this.startActivityForResult(intent, MESSAGE_PUBLISHED);
	}

}